﻿using System.Collections.Generic;
using System.Linq;
using Basmus.Conel;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Basmus.Tests
{
    [TestClass()]
    public class NullableBinderTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Дополнительные атрибуты теста
        // 
        //При написании тестов можно использовать следующие дополнительные атрибуты:
        //
        //ClassInitialize используется для выполнения кода до запуска первого теста в классе
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //ClassCleanup используется для выполнения кода после завершения работы всех тестов в классе
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //TestInitialize используется для выполнения кода перед запуском каждого теста
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //TestCleanup используется для выполнения кода после завершения каждого теста
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion
        

        [TestMethod()]
        public void BindNullableOfIntTest()
        {
            //Arrange
            var target = new NullableBinder();
            var containerType = typeof (int?);

            //Act
            var binding = target.Bind(containerType);

            //Assert
            Assert.IsNotNull(binding);
            Assert.IsInstanceOfType(binding, typeof (NullableBinding<int>));
        }

        [TestMethod()]
        public void BindNullableOfIntWithIntTest()
        {
            //Arrange
            var target = new NullableBinder();
            var containerType = typeof(int?);
            var elementType = typeof (int);

            //Act
            var binding = target.Bind(containerType, elementType);

            //Assert
            Assert.IsNotNull(binding);
            Assert.IsInstanceOfType(binding, typeof(NullableBinding<int>));
        }

        [TestMethod()]
        public void BindNullableOfIntWithDoubleTest()
        {
            //Arrange
            var target = new NullableBinder();
            var containerType = typeof(int?);
            var elementType = typeof(double);

            //Act
            var binding = target.Bind(containerType, elementType);

            //Assert
            Assert.IsNull(binding);
        }

        [TestMethod()]
        public void BindStringTest()
        {
            //Arrange
            var target = new NullableBinder();
            var containerType = typeof(string);
           
            //Act
            var binding = target.Bind(containerType);

            //Assert
            Assert.IsNull(binding);
        }

        [TestMethod()]
        public void BindStringWithIntTest()
        {
            //Arrange
            var target = new NullableBinder();
            var containerType = typeof(string);
            var elementType = typeof(int);

            //Act & Assert
            var binding = target.Bind(containerType, elementType);

            //Assert
            Assert.IsNull(binding);
        }

        /// <summary>
        /// Default binding of NullableBinder can bind only Nullables
        /// </summary>
        [TestMethod()]
        public void BindValueTypesTest()
        {
            //Arrange
            var target = new NullableBinder();
            var nullableContainerTypes = TypeHelper.ValueTypes().MakeGenericTypesForDefinition(typeof (Nullable<>));

            //Act
            bool selectedMatched = nullableContainerTypes.All(t => target.Bind(t) != null);
            bool hasOtherMatched = TypeHelper.EveryPossibleContainerType().Except(nullableContainerTypes).Any(c => target.Bind(c) != null);

            //Assert
            Assert.IsTrue(selectedMatched);
            Assert.IsFalse(hasOtherMatched);
        }

        [TestMethod()]
        public void BindTest()
        {
            var target = new NullableBinder();
            var elementsTypes = TypeHelper.ValueTypes();
            foreach (var elementsType in elementsTypes)
            {
                var containerType = typeof (Nullable<>).MakeGenericType(elementsType);

                var expectedBindingType = typeof (NullableBinding<>).MakeGenericType(elementsType);

                //Act
                var actualBinding = target.Bind(containerType);

                //Assert
                Assert.IsInstanceOfType(actualBinding, expectedBindingType);

                //Act
                actualBinding = target.Bind(containerType, elementsType);

                //Assert
                Assert.IsInstanceOfType(actualBinding, expectedBindingType);
            }
        }
    }
}
